﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Security.Principal;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace yysAuxiliary
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            if (IsAdministrator())
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new MainFrm());//MainFrm()
            }
            else
            {
                //如果不具有管理员权限，则以管理员身份执行可执行程序
                var startInfo = new ProcessStartInfo
                {
                    FileName = Application.ExecutablePath,//进程目标文件
                    Arguments = string.Join(" ", args),//进程参数
                    Verb = "runas"//管理员权限
                };
                Process.Start(startInfo);
            }
        }

        /// <summary>
        /// 是否是管理员
        /// </summary>
        /// <returns></returns>
        internal static bool IsAdministrator()
        {
            var identity = WindowsIdentity.GetCurrent();
            var principal = new WindowsPrincipal(identity);
            return principal.IsInRole(WindowsBuiltInRole.Administrator);
        }
    }
}
